package day1129_1;

import java.sql.Connection;
import java.sql.DriverManager;

public class C2 {
    //饮水机
    private   static  final  ThreadLocal<Connection> threadLocal
                                        =new ThreadLocal<Connection>();

    public static  Connection  getConnection()throws  Exception{
        Connection  conn=threadLocal.get();//1.从threadLocal获取连接对象，能获取到就使用(获取不到再创建)
        if(conn==null){//2.如果获取不到连接再创建数据库连接
            Class.forName("com.mysql.cj.jdbc.Driver");//2.1.
            Connection  conn2= DriverManager.getConnection("jdbc:mysql://localhost:3306/big1?serverTimezone=GMT%2B8&useSSL=false",
                    "root","root");//2.2.
            threadLocal.set(conn2);//3.将创建的连接存入到threadLocal中
            return  conn2;
        }
        return  conn;//2.如果获取到连接对象，就返回连接
    }

    public static void main(String[] args) throws Exception {
        System.out.println( getConnection());
    }


}







